package com.situ.hotel.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.hotel.entity.Emp;
import com.situ.hotel.util.JDBCUtil;
import com.situ.hotel.util.JSONResult;
import com.situ.hotel.util.JSONUtil;

import sun.rmi.log.LogOutputStream;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 结局post请求乱码问题
		req.setCharacterEncoding("UTF-8");
		// /loginServlet?method=login
		// /loginServlet?method=logout
		String method = req.getParameter("method");
		switch (method) {
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		default:
			break;
		}
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		Emp emp = (Emp)session.getAttribute("emp");
		if (emp != null) {
			// 销毁Session
			session.invalidate();
			// 重定向到登录界面
			resp.sendRedirect(req.getContextPath() + "/web/login.jsp");
		}
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) {
		String searchCode = req.getParameter("code");
		String searchPassword = req.getParameter("password");
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Emp emp = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`code`,`name`,`birthday`,`address`,`phone`,`password`,`dept_code` FROM `emp` WHERE `code`=? AND `password`=? ";
			statement = connection.prepareStatement(sql);
			statement.setString(1, searchCode);
			statement.setString(2, searchPassword);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String code = resultSet.getString("code");
				String name = resultSet.getString("name");
				String address = resultSet.getString("address");
				String phone = resultSet.getString("phone");
				String password = resultSet.getString("password");
				Date birthday = resultSet.getDate("birthday");
				String deptCode = resultSet.getString("dept_code");
				emp = new Emp(id, code, name, birthday, address, phone, password, deptCode);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		if (emp != null) {// 登录成功
			// 登录成功会把用户信息放到Session，在Filter里面从Session里面拿出来看有没有
			HttpSession session = req.getSession();
			session.setAttribute("emp", emp);
			
			JSONResult jsonResult = JSONResult.success("登录成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("登录失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
		
	}
}
